package com.it00zyq.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it00zyq.admin.param.ScheduleParam;
import com.it00zyq.admin.service.ScheduleService;
import com.it00zyq.common.entity.Article;
import com.it00zyq.common.entity.Schedule;
import com.it00zyq.common.entity.Type;
import com.it00zyq.common.entity.User;
import com.it00zyq.common.enums.DeletedEnum;
import com.it00zyq.common.enums.ScheduleEnum;
import com.it00zyq.common.mapper.ScheduleMapper;
import com.it00zyq.common.mapper.TypeMapper;
import com.it00zyq.common.param.ListParam;
import com.it00zyq.common.utils.ApiException;
import com.it00zyq.common.utils.JwtUtil;
import com.it00zyq.common.utils.PageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @Author zyq
 * @Date 2022/4/29 16:42
 * @Description TODO
 * @Version 1.0
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class ScheduleServiceImpl implements ScheduleService {
    private final ScheduleMapper scheduleMapper;
    private final JwtUtil jwtUtil;

    @Override
    public void add(ScheduleParam param, String token) {
        User user = jwtUtil.getUser(token);
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(param, schedule);
        schedule.setDeleted(DeletedEnum.NORMAL.getCode());
        schedule.setUserId(user.getId());
        if (ScheduleEnum.PROCESSING.getCode().equals(param.getState())) {
            schedule.setStartTime(new Date());
        }
        if (ScheduleEnum.FINISHED.getCode().equals(param.getState())) {
            schedule.setActualTime(new Date());
        }
        scheduleMapper.insert(schedule);
    }

    @Override
    public void modify(ScheduleParam param) {
        Schedule schedule = scheduleMapper.selectById(param.getId());
        if (ObjectUtils.isEmpty(schedule)) {
            throw new ApiException("日程信息不存在");
        }
        BeanUtils.copyProperties(param, schedule);
        if (ScheduleEnum.PROCESSING.getCode().equals(param.getState())) {
            schedule.setStartTime(new Date());
        }
        if (ScheduleEnum.FINISHED.getCode().equals(param.getState())) {
            schedule.setActualTime(new Date());
        }
        scheduleMapper.updateById(schedule);
    }

    @Override
    public void delete(Integer id) {
        scheduleMapper.deleteById(id);
    }

    @Override
    public PageVO list(ListParam param, String token) {
        User user = jwtUtil.getUser(token);
        QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getId());
        if (!ObjectUtils.isEmpty(param.getType())) {
            wrapper.eq("type", param.getType());
        }
        if (!ObjectUtils.isEmpty(param.getState())) {
            wrapper.eq("state", param.getState());
        }

        // 未完成或进行中，且即将达到截止日期的日程排在前面
        wrapper.orderByAsc("state");
        wrapper.orderByAsc("estimated_time");

        Page<Schedule> page = new Page<>(param.getPageNum(), param.getPageSize());
        IPage<Schedule> schedulePage = scheduleMapper.selectPage(page, wrapper);
        return PageVO.convert(schedulePage);
    }
}
